package com.chuangmei.com.arindoorlocationsystem.config.fragment;

import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chuangmei.com.arindoorlocationsystem.R;
import com.chuangmei.com.arindoorlocationsystem.base.adapter.BaseRecyclerViewAdapter;
import com.chuangmei.com.arindoorlocationsystem.config.adapter.ConfigAdapter;
import com.chuangmei.com.arindoorlocationsystem.base.BaseFragment;
import com.chuangmei.com.arindoorlocationsystem.bean.MapItem;
import com.chuangmei.com.arindoorlocationsystem.config.ConfigMessageActivity;
import com.chuangmei.com.arindoorlocationsystem.utils.CacheUtil;
import com.chuangmei.com.arindoorlocationsystem.utils.Constant;
import com.chuangmei.com.arindoorlocationsystem.utils.DialogUtils;
import com.chuangmei.com.arindoorlocationsystem.utils.LogUtils;
import com.chuangmei.com.arindoorlocationsystem.dialog.ProgressDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：ARIndoorLocationSystem
 * 类描述：配置列表页面
 * 创建人：LuoHao
 * 创建时间：2018/4/8 16:03
 * 修改人：LuoHao
 * 修改时间：2018/4/8 16:03
 * 修改备注：
 */

public class ConfigListFragment extends BaseFragment {

    @Override
    protected int setLayout() {
        return R.layout.fragment_config_list;
    }

    @Override
    protected void initView(View view) {
        RecyclerView mRV_config = (RecyclerView) view;
        mRV_config.setLayoutManager(new LinearLayoutManager(getContext()));
        final List<MapItem> list = new ArrayList<>();
        for (Integer integer : Constant.configItemName) {
            MapItem mapItem = null;
            if (integer == Constant.configItemName[2]) {//缓存值填充
                try {
                    mapItem = new MapItem(getContext().getResources().getString(integer), CacheUtil.getTotalCacheSize
                            (getContext()));
                } catch (Exception e) {
                    LogUtils.debug(e.getMessage());
                }
            } else {
                mapItem = new MapItem(getContext().getResources().getString(integer), null);
            }
            if (mapItem != null) {
                list.add(mapItem);
            }
        }
        final ConfigAdapter configAdapter = new ConfigAdapter(getContext(), R.layout.item_config_layout, list);
        mRV_config.setAdapter(configAdapter);
        //添加Android自带的分割线
        mRV_config.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));

        configAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                FragmentManager fm = getActivity().getSupportFragmentManager();

                //注意v4包的配套使用
                Fragment fragment = null;
                switch (Constant.configItemName[position]) {
                    case R.string.help_title:
                        fragment = new HelpFragment();
                        break;
                    case R.string.app_message_title:
                        fragment = new AppMessageFragment();
                        break;
                    case R.string.share_title:
                        fragment = new ShareFragment();
                        break;
                    case R.string.jump_title:
                        fragment = new JumpAppFragment();
                        break;
                    case R.string.delete_cache:
                        try {
                            //判定是否有缓存
                            final String cacheSize = CacheUtil.getTotalCacheSize(getContext());

                            if (cacheSize != "0KB") {
                                //弹出是否删除窗口
                                DialogUtils dialogUtils = new DialogUtils();
                                dialogUtils.showNoTitleDialog(getContext(), R.string.is_delete_cache, true,
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                //确定删除，弹出等待Dialog
                                                final ProgressDialog progressDialog = new ProgressDialog(getContext());
                                                progressDialog.show();
                                                new Thread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        //删除缓存文件
                                                        CacheUtil.clearAllCache(getContext());
                                                        try {//等待三秒
                                                            Thread.sleep(3000);
                                                            //主线程刷新UI，关闭等待Dialog
                                                            getActivity().runOnUiThread(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                    MapItem mapItem = list.get(2);
                                                                    mapItem.setValue("0K");
                                                                    configAdapter.notifyDataSetChanged();
                                                                    progressDialog.dismiss();
                                                                }
                                                            });
                                                        } catch (InterruptedException e) {
                                                            LogUtils.debug("线程暂停错误");
                                                        }
                                                    }
                                                }).start();
                                            }
                                        }, null);
                            }
                        } catch (Exception e) {
                            LogUtils.showToastShort("清除失败");
                        }
                    default:
                        break;
                }
                try {
                    fm.beginTransaction().add(R.id.mFL_config, fragment).addToBackStack(null).commit();

                } catch (NullPointerException e) {
                    LogUtils.debug(Constant.configItemName[position] + "Fragment创建失败");
                }
            }

            @Override
            public void onLongClick(int position) {
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        ((ConfigMessageActivity) getActivity()).chengTitle(getResources().getString(R.string.config_title));
    }

}
